﻿/********************************************************************************************************
 * ConfigurationHelper.cs
 * A simple configuration values store cache using the singleton pattern.
 * Jaime Alberto Jaramillo (jjaramillo@informatica-tecnologia.com
 * Created: 13/02/2012
 * Updated: 14/02/2012
 ********************************************************************************************************/

using System.Collections.Generic;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;
using System.Web;

namespace MS.InformationManagement.Support
{
    /// <summary>
    /// A simple configuration values store cache using the singleton pattern.
    /// </summary>
    public class ConfigurationHelper
    {
        private string _siteUrl = string.Empty;
        private static object _Lock = new object();
        private Dictionary<string, string> _ConfigItems;
        private static ConfigurationHelper _Instance;

        /// <summary>
        /// Gets the config items.
        /// </summary>
        public Dictionary<string, string> ConfigItems
        {
            get
            {
                //if (_ConfigItems == null)
                //{
                LoadDataIntoCache();
                //}
                return _ConfigItems;
            }
        }

        /// <summary>
        /// Prevents a default instance of the <see cref="ConfigurationHelper"/> class from being created.
        /// </summary>
        private ConfigurationHelper()
        {
        }

        /// <summary>
        /// Prevents a default instance of the <see cref="ConfigurationHelper"/> class from being created.
        /// </summary>
        private ConfigurationHelper(string siteUrl)
        {
            _siteUrl = siteUrl;
        }

        /// <summary>
        /// Gets Configuration helper instance.
        /// </summary>
        /// <returns></returns>
        public static ConfigurationHelper GetInstance()
        {
            //if (_Instance == null)
            //{
            _Instance = new ConfigurationHelper();
            //}
            return _Instance;
        }

        /// <summary>
        /// Gets Configuration helper instance.
        /// </summary>
        /// <returns></returns>
        public static ConfigurationHelper GetInstance(string siteUrl)
        {
            //if (_Instance == null)
            //{
            _Instance = new ConfigurationHelper(siteUrl);
            //}
            return _Instance;
        }

        /// <summary>
        /// Loads the data into the cache.
        /// </summary>
        private void LoadDataIntoCache()
        {
            //lock (_Lock)
            //{
            if (_ConfigItems == null)
            {
                SPSite site = null;
                bool mustDispose = false;
                _ConfigItems = new Dictionary<string, string>();
                SPWeb currentWeb = SPContext.Current == null ? null : SPContext.Current.Web;
                if (currentWeb == null)
                {
                    mustDispose = true;
                    //TODO: Pilas aqui
                    site = new SPSite(_siteUrl);
                    currentWeb = site.OpenWeb();
                }
                SPList configItemsList = currentWeb.Lists[GlobalConstants.LIST_CONFIGURATION];
                SPListItemCollection items = configItemsList.Items;
                foreach (SPListItem currentConfigItem in items)
                {
                    _ConfigItems.Add(currentConfigItem[SPBuiltInFieldId.Title].ToString(),
                        currentConfigItem[CustomSiteColumns.Value].ToString());
                }
                if (mustDispose)
                {
                    currentWeb.Close();
                    currentWeb.Dispose();
                    site.Close();
                    site.Dispose();
                }
            }
            //}
        }

        /// <summary>
        /// Cleans the cache.
        /// </summary>
        private void CleanCache()
        {
            lock (_Lock)
            {
                if (_ConfigItems != null)
                {
                    _ConfigItems.Clear();
                    _ConfigItems = null;
                }
            }
        }

        /// <summary>
        /// Reloads the cache.
        /// </summary>
        public void ReloadCache()
        {
            CleanCache();
            LoadDataIntoCache();
        }
    }
}
